#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <stdio.h>

#include<iostream>
#include <gst/gst.h>
#include <stdio.h>

#include <sstream>
#include <string>
#include <QtCore/QTimer>
#include <QtCore/QTime>

#define AUDIO_CAPS "application/x-rtp,media=audio, clock-rate=89000, width=16, height=16, encoding-name=L16, payload=96"

using namespace std;

GstElement *pipeline;
GstElement *volume;
string audioPath;
float volumen = 1.0;
QTimer timePosition;

void MainWindow::updateSlider(bool playing)
{
    if(playing){
        timePosition.start(700);
    } else {
        timePosition.stop();
    }
}

/* This method executes to listen to a determine port where the server will send the mp3 song*/
void receiveMp3Audio ()
{

    /*Main object*/
       GMainLoop *loop;

       /*Elements objects*/
       GstElement *source,
                  *decoder,
                  *convert,
                  *sink;

       /*Cap object*/
       GstCaps *caps;

       loop = g_main_loop_new (NULL, FALSE);

       /* Create gstreamer elements */
       pipeline = gst_pipeline_new ("audio-player");
       source   = gst_element_factory_make ("udpsrc","udpsrc");
       decoder  = gst_element_factory_make ("rtpL16depay","rtpL16depay");
       convert  = gst_element_factory_make ("audioconvert","audioconvert");
       sink     = gst_element_factory_make ("alsasink","alsasink");

       volume   =  gst_element_factory_make ("volume","volume");


       if (!pipeline || !source || !decoder || !convert  || !volume || !sink) {
           g_printerr ("One element could not be created. Exiting.\n");
       }

       caps = gst_caps_from_string (AUDIO_CAPS);
       g_object_set (G_OBJECT (volume), "volume", volumen, NULL);

       g_object_set (G_OBJECT (source), "port", 5000, NULL);
       g_object_set (G_OBJECT (source), "caps", caps , NULL);


       /* we add all elements into the pipeline */
       gst_bin_add_many (GST_BIN (pipeline),source, decoder, convert,volume,sink, NULL);

       /* we link the elements together */
       gst_element_link_many (source, decoder, convert,volume,sink, NULL);


       /* Set the pipeline to "playing" state*/
       g_print ("Now playing...\n");
       gst_element_set_state (pipeline, GST_STATE_PLAYING);

       /* Iterate */
       g_print ("Listening...\n");
       g_main_loop_run (loop);

       /* Stop playback */
       g_print ("Stopping playback\n");
       gst_element_set_state (pipeline, GST_STATE_NULL);

       /* Delete the pipeline */
       g_print ("Deleting pipeline\n");
       gst_object_unref (GST_OBJECT (pipeline));
       g_main_loop_unref (loop);


}


void stop_audio()
{
    gst_element_set_state (pipeline, GST_STATE_PAUSED);
    gst_element_set_state (pipeline, GST_STATE_NULL);
    g_print ("Deleting pipeline\n");
    gst_object_unref (GST_OBJECT (pipeline));

}


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QObject::connect(ui->spinBox,SIGNAL(valueChanged(int)),ui->dial,SLOT(setValue(int)));
    QObject::connect(ui->dial,SIGNAL(valueChanged(int)),ui->spinBox,SLOT(setValue(int)));
    QObject::connect(&timePosition, SIGNAL(timeout()), this, SLOT(updateTimer()));
}

MainWindow::~MainWindow()
{
    delete ui;

}



/* This method executes when the Stop button is pressed*/
void MainWindow::on_buttonStop_clicked()
{

}


/* This method executes when the Play button is pressed*/
void MainWindow::on_buttonPlay_clicked()
{
    receiveMp3Audio();

}

void MainWindow::on_dial_sliderMoved()
{
    volumen = ui->spinBox->value()*0.01f;
    g_object_set (G_OBJECT (volume), "volume", volumen, NULL);
}

void MainWindow::on_spinBox_valueChanged()
{
    volumen = ui->spinBox->value()*0.01f;
    g_object_set (G_OBJECT (volume), "volume", volumen, NULL);
}

void MainWindow::updateTimer()
{
    int position = 0;
    gint64 pos;
    GstFormat format = GST_FORMAT_TIME;
    if (gst_element_query_position (pipeline, &format, &pos)){
        position =  pos / GST_SECOND;
    }
    ui->horizontalSlider->setSliderPosition(position);
 }
